﻿using System;
using System.Text;
using System.Windows.Forms;
using IntfClass;

namespace IntfSetting {
    public partial class FrmBuinessNew : Form {
        public bool blnNew = true;
        public long ID = 0;
        public string strCode = string.Empty;
        public string strName = string.Empty;

        public FrmBuinessNew() {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e) {
            if (!CheckData()) return;

            var strSql = new StringBuilder();

            if (blnNew) {
                strSql.AppendLine("Insert into TBusiness(Code,Name) values ('" + txtCode.Text + "'," + "'" +
                                  txtName.Text + "')");
            }
            else {
                strSql.AppendLine("Update TBusiness set Code ='" + txtCode.Text + "'," + " Name = '" + txtName.Text +
                                  "'");
                strSql.AppendLine("Where ID = " + ID);
            }

            var classDB = new ClassDB();
            if (classDB.ExecuteTransaction(strSql.ToString())) {
                MessageBox.Show("保存成功！");
                Dispose();
            }
        }

        private bool CheckData() {
            try {
                var blnReturn = true;
                if (txtCode.Text.Trim().Equals(string.Empty)) {
                    MessageBox.Show("编号不可为空");
                    txtCode.Focus();
                    return false;
                }

                if (txtName.Text.Trim().Equals(string.Empty)) {
                    MessageBox.Show("名称不可为空");
                    txtName.Focus();
                    return false;
                }

                return blnReturn;
            }
            catch (Exception) {
                return false;
                throw;
            }
        }

        private void btnCancle_Click(object sender, EventArgs e) {
            Dispose();
        }

        private void FrmBuinessNew_Load(object sender, EventArgs e) {
            if (!blnNew) {
                txtCode.Text = strCode;
                txtName.Text = strName;
            }
        }
    }
}